<div class="widget">
	<div class="widget-header bg-blue">
		<i class="widget-icon icon-notebook"></i> 
		<span class="widget-caption"> 查看与编辑 </span>
		<div class="widget-buttons">
			<a href="javascript:void(0);" onclick="handlerCancel(this);" title="关闭"
				 data-toggle="dispose" data-refresh-table="#table-contract"> <i class="fa fa-times"></i> </a> 
		</div>
	</div>
	<div class="widget-body">
		<div class="callout callout-success">
		  <p>本合同编辑页面，用于修改购买课程类型、合同起止时间、已用课时、合同签约主体（宝宝、家长及手机）等信息。</p>
		  <h6>除上述信息外、不建议使用人员，通过本页面，直接修改合同金额、购买课时数（月数）等信息。</h6>
		</div>
		
		<!-- TODO 购买历史 -->
		<div class="widget radius-bordered mgbt-xs-10" id="editor-panel-history">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 购买历史 </span>
			</div>
			<div class="widget-body editor-body-sm">
				<table data-ajax="handlerSearch" data-method="post"
							 data-id-field="id" 
							 data-unique-id="id" 
							 data-pagination="true"  
							 data-check-on-init="true" 
							 data-side-pagination="server" 
							 data-mobile-responsive="true"
							 data-form-search="#search-form-history" id="table-history">
					<thead>
						<tr>
							<th data-checkbox="true"></th>
							<th data-field="student" 		data-align="center">宝宝姓名</th>
							<th data-field="item" 			data-align="center">购买项目</th>
							<th data-field="type" 			data-align="center">项目类型</th>
							<th data-field="createDate" data-align="center">购买时间</th>
							<th data-field="fromDate" 	data-align="center" data-formatter="renderFromOver">起止时间</th>
							<th data-field="totals" 		data-align="center">总课时</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
		
		<!-- TODO 基本信息 -->
		<div class="widget radius-bordered mgbt-xs-10">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 基本信息 </span>
			</div>
			<div class="widget-body editor-body-sm editor-body-contract">
				<!-- dynamic data -->
			</div>
		</div>
		
		<!-- TODO 购买信息 -->
		<div class="widget radius-bordered mgbt-xs-10" id="editor-panel-buyinfo">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 购买信息 </span>
			</div>
			<div class="widget-body editor-body-sm editor-body-buyinfo">
				<!-- dynamic data -->
			</div>
		</div>
		
		<!-- TODO 付款信息 -->
		<div class="widget radius-bordered mgbt-xs-10" id="editor-panel-payinfo">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 付款信息 </span>
			</div>
			<div class="widget-body editor-body-sm editor-body-payinfo">
				<!-- dynamic data -->
			</div>
		</div>

	</div>
</div>

<!-- 提交表单 ========================================================= -->
<form id="editor-form-contract" action="/miwawa/{dbname}/common/update" method="post" 
			name="com.app.miwawa.api.esm.entity.contract.Contract">
</form>
<form id="editor-form-buyinfo" action="/miwawa/{dbname}/common/update" method="post" 
			name="com.app.miwawa.api.esm.entity.contract.Buyinfo">
			<input type="hidden" name="bean['status']" value="0">
			<input type="hidden" name="bean['invalid']" value="F">
			<input type="hidden" name="bean['contractId']" data-foreign-id>
</form>
<form id="editor-form-payinfo" action="/miwawa/{dbname}/common/update" method="post" 
			name="com.app.miwawa.api.esm.entity.contract.Payinfo">
			<input type="hidden" name="bean['status']" value="0">
			<input type="hidden" name="bean['invalid']" value="F">
			<input type="hidden" name="bean['contractId']" data-foreign-id>
</form>

<script type="text/javascript">
<!--
	require(['domReady'],function(domReady){
		domReady(function () {
			jQuery("#search-form-buyinfo").data("template-id","#edit-buyinfo-template");
			jQuery("#search-form-payinfo").data("template-id","#edit-payinfo-template");
			
			initData();
		});
	});
	
	// TODO 加载合同数据
	var contract = jQuery("#table-contract").data("selectedRow");
	function initData(){
		jQuery(":hidden[name='contractId'],[data-foreign-id]").val(contract.id);
		jQuery(":hidden[name='studentid']").val(contract.studentid);
		
		// 加载基本信息
		jQuery(".editor-body-contract").loadform("load", {
			templateId: "#edit-contract-template",
			templatePath: "/miwawa/content/esm/contract/template.html",
			templateData: contract,
			templateHelper:helperStudent(contract.customerid),
			onLoadSuccess:function(options){
				require(['jquery.bootstrap.editable'],function(editable){
					editable.register("[data-editable]");
				});
			}
		});

		// 加载购买信息
		if(contract.type){
			jQuery("#search-form-buyinfo").loadform("many",helperBuyinfo());
		}else{
			jQuery("#search-form-buyinfo").loadform("empty",{
				templateData:[{"type":contract.item}],
				templateHelper:helperBuyinfo()
			});
		}
		
		// 加载付款信息
		if(contract.payid){
			jQuery("#search-form-payinfo").loadform("many",helperPayinfo());
		}else{
			jQuery("#search-form-payinfo").loadform("empty",{
				templateData:[{}],
				templateHelper:helperPayinfo()
			});
		}
		
		// 加载购买历史
		jQuery("#table-history").bootstrapTable({
			onLoadSuccess:function(data){
				require(['jquery.plugin.form'],function(form){
					form.datepicker(".date-selector");
					form.tooltip("[data-toggle='tooltip']");
				});
			}
		});
	}
	
	// 判断合同结束时间不得早于开始时间
	function validate(button,table,form){
		if(jQuery("#fromDate",form).val() == "" || jQuery("#overDate",form).val() == ""){
	   		jQuery.message.alert("请填写合同有效期");
	   		return;		    	
	  }else{
	   	var fromDate = $moment(jQuery("#fromDate",form).val()).format("YYYY-MM-DD")+" 01:00:00";
	   	var overDate = $moment(jQuery("#overDate",form).val()).format("YYYY-MM-DD")+" 02:00:00";
	   	var nowDate = $moment().format("YYYY-MM-DD")+" 00:00:00";
	   	if($moment(fromDate).isAfter($moment(overDate))){
	   		jQuery.message.alert("合同结束时间不能早于起始时间");
	   		return;
	   	}else if($moment(nowDate).isAfter($moment(overDate))){
	   		jQuery.message.alert("合同结束时间不能早于当前时间");
	   		return;
	   	}else{
	    	handlerSubmit(button, table, form);
	   	}
	  }
	}
	
	// 判断上课结束时间不得早于开始时间，总课时必填
	function determine(button,table,form){
		var fromDate = $moment(jQuery("#fromDate",form).val()).format("YYYY-MM-DD")+" 01:00:00";
   	var overDate = $moment(jQuery("#overDate",form).val()).format("YYYY-MM-DD")+" 02:00:00";
   	var nowDate = $moment().format("YYYY-MM-DD")+" 00:00:00";
   	if($moment(fromDate).isAfter($moment(overDate))){
   		jQuery.message.alert("上课结束时间不能早于起始时间");
   		return;
   	}else if($moment(nowDate).isAfter($moment(overDate))){
   		jQuery.message.alert("上课结束时间不能早于当前时间");
   		return;
   	}else if(jQuery("#totals",form).val() == ""){   		
   		jQuery.message.alert("请填写总课时或总月数");  	
   		return;
   	}else{
    	handlerSubmit(button, table, form);
   	}
	}
	
	// 更新客户状态
	function updateCustomer(){
		jQuery.ajaxsend({
			url:"/miwawa/{dbname}/customer/update".replace("{dbname}", dbname),
			data:{ "id":contract.customerid, "status":"member" }
		});
	}
	
	//付款信息的检查
	function checkPayinfo(button,table,form){
		if(jQuery("#priceOrig",form).val() == ""){
	   	jQuery.message.alert("请填写原价！");
	   	return;
	  }else if(jQuery("#priceReal",form).val() == ""){
	   	jQuery.message.alert("请填写实价！");
	   	return;
	  }else if(jQuery("#received",form).val() == ""){
		  jQuery.message.alert("请填写实收！");	
	   	return;	   
	  }else{
		  updateCustomer();
	    handlerSubmit(button, table, form);
	  }
	}
//-->
</script>